/**
 用到的listviewItem 是lmy_all_books_item.xml
 */
package org.buptsse.ui;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;

import org.buptsse.entity.Circle;
import org.buptsse.entity.Post;
import org.buptsse.entity.User;
import org.buptsse.utils.Action;
import org.buptsse.utils.ImageService;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class SearchChannelActivity extends ListActivity implements OnScrollListener,OnClickListener{
	private final String URL_GETCIRCLEPOST = "getCirclePost.do";
	private final String SAVE_PATH = "/mnt/sdcard/tmp/";
	private final static int FIRST = 0;
	private final static int MORE = 1;
	private final static int REFRESF = -1;
	// Button footerButton;
	// LinearLayout footerProgressBarLayout;// 加载进度滚动条
	// View view;
	ListAdapter listAdapter = new ListAdapter();
	ListView list;
	int visibleItemCount;
	private final String PROJECT_NAME = "BIRD";
	
	int scrollState;
	List<Post> jsonList = new ArrayList<Post>();
	int lastItem;
	int count;
	private User user = null;
	private Circle circle;
	private Button manage;
	private Button rd_back,rd_home,rd_post;

	private TextView tvName = null;
	private TextView tvDesc = null;
	private TextView tvPostNumber = null;
	SharedPreferences preferences = null;

	Handler myHandler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case SearchChannelActivity.FIRST:
				if (null != jsonList) {
					listAdapter.count = jsonList.size();
				} else {
					listAdapter.count = 0;
				}
				listAdapter.notifyDataSetChanged();
				break;
			case SearchChannelActivity.MORE:
				if (null != jsonList) {
					listAdapter.count = jsonList.size();
				} else {
					listAdapter.count = 0;
				}
				listAdapter.notifyDataSetChanged();
				break;
			case SearchChannelActivity.REFRESF:
				break;
			}
			super.handleMessage(msg);
		}
	};

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Intent it = getIntent();
		setContentView(R.layout.lsearch_channel);
		
			
		

//		LinearLayout down = (LinearLayout) findViewById(R.id.down);
//		down.setVisibility(View.GONE);
//		rd_back = (Button)findViewById(R.id.rd_back);
//		rd_home = (Button)findViewById(R.id.rd_home);
//		rd_post = (Button)findViewById(R.id.rd_post);
		//manage = (Button) findViewById(R.id.manage_circle);
//		tvName = (TextView) findViewById(R.id.txt_wb_item_uname);
//		tvDesc = (TextView) findViewById(R.id.txt_wb_item_content);
//		tvPostNumber = (TextView) findViewById(R.id.txt_wb_item_comment);
//
//		user = (User) it.getSerializableExtra("user");
//		circle = (Circle) it.getSerializableExtra("onSelect");
//		
//		rd_back.setOnClickListener(this);
//		rd_home.setOnClickListener(this);
//		rd_post.setOnClickListener(this);
//		
//		int flag = circle.getLevel();
//		tvName.setText(circle.getCircleName());
//		tvDesc.setText(circle.getDescription());
//		tvPostNumber.setText("" + circle.getAmount());
//		if (flag == 1) {
//			manage.setOnClickListener(new OnClickListener() {
//				@Override
//				public void onClick(View v) {
//					Intent it = new Intent(MessageActivity.this,LogoActivity.class);
//					it.putExtra("user", user);
//					it.putExtra("circle", circle);
//					MessageActivity.this.startActivity(it);
//				}
//			});
//		}else{
//			manage.setVisibility(View.GONE);
//		}
//		listAdapter.count = 0;
//
//		list = getListView();
//		// list.addFooterView(view);
//		list.setAdapter(listAdapter);
//		list.setOnScrollListener(this);
//		list.setOnItemClickListener(new OnItemClickListener() {
//
//			@Override
//			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
//					long arg3) {
//				// TODO Auto-generated method stub
//				Post onSelected = jsonList.get(arg2);
//				Intent it = new Intent(MessageActivity.this, LogoActivity.class);
//				it.putExtra("onSelected", onSelected);
//				it.putExtra("user", user);
//				MessageActivity.this.startActivity(it);
//			}
//		});
//
//		list.setOnScrollListener(new OnScrollListener() {
//			@Override
//			public void onScrollStateChanged(AbsListView view, int scrollState) {
//				// TODO Auto-generated method stub
//				if (scrollState == OnScrollListener.SCROLL_STATE_IDLE) {
//					if (view.getLastVisiblePosition() == view.getCount() - 1) {
//						// 1表示获取更多
//						Runnable run = new myThread(MORE);
//						new Thread(run).start();
//					} else {
//						Runnable run = new myThread(REFRESF);
//						new Thread(run).start();
//					}
//				} else if (scrollState == OnScrollListener.SCROLL_STATE_FLING) {
//					System.out.println("SCROLL_STATE_FLING");
//				} else if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
//					System.out.println("SCROLL_STATE_TOUCH_SCROLL");
//				}
//			}
//
//			@Override
//			public void onScroll(AbsListView view, int firstVisibleItem,
//					int visibleItemCount, int totalItemCount) {
//				// TODO Auto-generated method stub
//
//			}
//		});
//
//		Runnable run = new myThread(FIRST);
//		// myHandler.post(run);
//		new Thread(run).start();
	}

	private class myThread implements Runnable {
		int flag;
		public myThread(int flag) {
			this.flag = flag;
		}
		@Override
		public void run() {
			Message message = new Message();

			if (null == jsonList || jsonList.size() == 0) {
				jsonList = new Action().GetCirclePost(URL_GETCIRCLEPOST,
						circle.getCircleId(), user.getId(), 0, flag);
			} else if (flag == 1) {
				jsonList.addAll(new Action().GetCirclePost(URL_GETCIRCLEPOST,
						circle.getCircleId(), user.getId(),
						jsonList.get(jsonList.size() - 1).getPostId(), flag));
			} else {
				jsonList.addAll(new Action().GetCirclePost(URL_GETCIRCLEPOST,
						circle.getCircleId(), user.getId(), jsonList.get(0)
								.getPostId(), flag));
			}
			message.what = flag;
			myHandler.sendMessage(message);
		}
	}

	class ListAdapter extends BaseAdapter {
		int count = 10;
		@Override
		public int getCount() {
			return count;
		}
		@Override
		public Object getItem(int position) {
			return position;
		}
		@Override
		public long getItemId(int position) {
			return position;
		}
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			LayoutInflater inflater = LayoutInflater.from(SearchChannelActivity.this);
			View view = inflater.inflate(R.layout.other_listview_item, null);
//			TextView tv = (TextView) view
//					.findViewById(R.id.txt_wb_item_content);
//			Post tmp = jsonList.get(position);
//			tv.setText(tmp.getPostContent());
//			tv = (TextView) view.findViewById(R.id.txt_wb_item_time);
//			tv.setText(tmp.getPostTime());
//			tv = (TextView) view.findViewById(R.id.txt_wb_item_uname);
//			tv.setText(tmp.getUsername());
//			LinearLayout subpost = (LinearLayout) view
//					.findViewById(R.id.subpost);
//			// subcontent
//			if (tmp.getMessageType() == 0)
//				subpost.setVisibility(View.GONE);
//			else {
//				tv = (TextView) view.findViewById(R.id.txt_wb_item_subcontent);
//				tv.setText(tmp.getOriginalPostContent());
//				ImageView headImage = (ImageView) view
//						.findViewById(R.id.img_wb_item_content_subpic);
//				byte[] data = null;
//				try {
//					data = ImageService.getImage(SAVE_PATH
//							+ tmp.getImage());
//				} catch (Exception e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
//				Bitmap bm = null;
//				if (null != data) {
//					headImage.setVisibility(View.VISIBLE);
//					bm = BitmapFactory.decodeByteArray(data, 0, data.length);
//					headImage.setImageBitmap(bm);
//				} else {
//					headImage.setVisibility(View.GONE);
//				}
//			}
//
//			tv = (TextView) view.findViewById(R.id.txt_wb_item_redirect);
//			tv.setText("" + tmp.getRepostAmount());
//			tv = (TextView) view.findViewById(R.id.txt_wb_item_comment);
//			tv.setText("" + tmp.getCommentsAmount());
//
//			// head image
//			ImageView headImage = (ImageView) view
//					.findViewById(R.id.img_wb_item_head);
//
//			Bitmap bm = null;
//
//			File f = new File(SAVE_PATH + tmp.getImage());
//			if (f.exists()) {
//				bm = decodeFile(f);
//				headImage.setImageBitmap(bm);
//			}
//
//			// image
//			ImageView Image = (ImageView) view
//					.findViewById(R.id.img_wb_item_content_pic);
//			byte[] data1 = null;
//			try {
//				data1 = ImageService.getImage(SAVE_PATH
//						+ tmp.getLink());
//			} catch (Exception e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//			Bitmap bm1 = null;
//			if (null != data1) {
//				Image.setVisibility(View.VISIBLE);
//				bm1 = BitmapFactory.decodeByteArray(data1, 0, data1.length);
//				Image.setImageBitmap(bm1);
//			} else {
//				Image.setVisibility(View.GONE);
//			}
//
			return view;
		}
	}
	
	
	@Override
	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {
		this.visibleItemCount = visibleItemCount;
		lastItem = firstVisibleItem + visibleItemCount - 1;
		if (listAdapter.count >= count) {
			list.removeFooterView(view);
		}
	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		this.scrollState = scrollState;
	}

	@Override
	public void onClick(View v) {
		Intent it = new Intent();
		int vid=v.getId();
		System.out.println("click"+vid);
		switch(vid){
//		case R.id.rd_back:
//			it.putExtra("user", user);
//			MessageActivity.this.setResult(RESULT_OK, it);
//			MessageActivity.this.finish();
//			break;
//		case R.id.rd_home:
//			it.setClass(this, MainCenterActivity.class);
//			it.putExtra("user", user);
//			MessageActivity.this.startActivity(it);
//			break;
//		case R.id.rd_post:
//			it.setClass(this, LogoActivity.class);
//			it.putExtra("user", user);
//			it.putExtra("circle", circle);
//			MessageActivity.this.startActivity(it);
//			break;
		default:
			break;
		}
	}

}
